R 语言作图
(西交利物浦大学 健康与环境科学系)
1 课前预备
install.packages(c('beginr', 'Epi', 'ggplot2','Rcmdr', 'ggplotgui', 'GrapheR', 'plotly', 'GGally', 'remotes', 'MSG', 'fun'))
remotes::install_github('pzhaonet/fecitr')- 数据:准备自己的一个数据文件,以便课上练习。
2 课程目标
- 了解 R 语言在科研领域能绘制哪些图形
- 了解 R 语言作图的两种常见体系(即 base R 和 ggplot2 包)和两种实现手段(即图形界面和代码)
- 掌握常见统计图形的 R 语言方法
3 R 能做哪些图
3.1 从使用场景看
3.1.1 学术论文
3.1.2 演示文稿
3.1.3 其他效果
3.1.4 无处不在
3.2 从数据类型和统计方法看
赵鹏,谢益辉,黄湘云:现代统计图形
-
- 示例:小提琴图
4 两种实现手段
4.1 图形界面
- GrapheR 包
library(GrapheR)
run.GrapheR()- Rcmdr 包
library(Rcmdr)ggplotgui::ggplot_shiny()library(Deducer)
JGR()4.2 代码
data(diet, package = "Epi")
library(ggplot2)
ggplot(diet) +
geom_point(aes(height, weight))5 统计图形的构成
图形
- 绘图区
- 坐标轴
- 图例
编号和标题
6 常见图形的做法
6.1 概率密度图
curve(dnorm(x), from = -5, to = 5)
segments(c(-1, 1), 0, c(-1, 1), dnorm(c(-1, 1)), 'red')
curve(dt(x, df = 10), from = -5, to = 5)
abline(h = 0)
abline(v = qnorm(0.95), col = 'red')
curve(dchisq(x, df = 10), from = 0, to = 30)
abline(h = 0)
abline(v = qchisq(0.95, df = 10), col = 'red')| Distribution | p- |
q- |
d- |
r- |
|---|---|---|---|---|
| Binomial | pbinom |
qbinom |
dbinom |
rbinom |
| Chi-Square | pchisq |
qchisq |
dchisq |
rchisq |
| F | pf |
qf |
df |
rf |
| Logistic | plogis |
qlogis |
dlogis |
rlogis |
| Log Normal | plnorm |
qlnorm |
dlnorm |
rlnorm |
| Normal | pnorm |
qnorm |
dnorm |
rnorm |
| Poisson | ppois |
qpois |
dpois |
rpois |
| Student t | pt |
qt |
dt |
rt |
| Uniform | punif |
qunif |
dunif |
runif |
| Weibull | pweibull |
qweibull |
dweibull |
rweibull |
| Wilcoxon Rank Sum Statistic | pwilcox |
qwilcox |
dwilcox |
rwilcox |
| Wilcoxon Signed Rank Statistic | psignrank |
qsignrank |
dsignrank |
rsignrank |
6.2 条形图,柱状图 (Bar chart)
tab1 <- table(diet$job)
barplot(tab1, horiz = TRUE, las = 1)
ggplot(diet) +
geom_bar(aes(job)) +
coord_flip()
tab2 <- table(diet[, c('job', 'chd')])
barplot(tab2, xlab = 'CHD', beside = TRUE, col = 1:3,
legend.text = TRUE,
args.legend = list(
x = 8, y = 100,
legend = dimnames(tab2)$job,
bty = 'n', col = 1:3))
ggplot(diet) +
geom_bar(aes(chd, fill = job), position = 'dodge')
ggplot(diet) +
geom_bar(aes(chd, fill = job)) +
facet_grid( ~ energy.grp) # or facet_wrap( ~ energy.grp)6.3 直方图,箱线图,小提琴图
hist(diet$weight, freq = FALSE)
lines(density(diet$weight))
ggplot(diet) +
geom_histogram(aes(x = weight)) +
facet_grid(~job)
boxplot(diet$weight ~ diet$job)
diet_mean <- tapply(diet$weight, diet$job, mean, na.rm = TRUE)
points(1:3, diet_mean, pch = 4)
ggplot() +
geom_boxplot(aes(job, weight), data = diet) +
geom_point(aes(1:3, diet_mean))
library(vioplot)
vioplot(diet$weight, horizontal = TRUE)
points(mean(diet$weight, na.rm = TRUE), 1, pch = 4, col = "red")
ggplot(diet, aes(x = weight, y = 0)) +
geom_violin(fill = 'grey') +
geom_boxplot(width = 0.1) +
geom_point(aes(x = mean(diet$weight, na.rm = TRUE), y = 0), pch = 4)6.4 散点图
ggplot(diet, aes(height, weight)) +
geom_point(alpha = 0.1) +
# geom_bin2d()
geom_smooth(method = 'lm') +
labs(x = 'Height(cm)', y = 'Weight (km)') +
theme_bw()7 更多图形
7.1 Base R
| Function | Description |
|---|---|
barplot() |
Generate a bar chart. |
boxplot() |
Generate a box plot (also called box-and-whisker plot). |
contour() |
Generate a contour plot. |
coplot() |
Generate a conditional plot. |
curve() |
Generate a curve corresponding to a function. |
dotchart() |
Generate a Cleveland dot plot. |
hist() |
Generate a histogram. |
image() |
Generate a grid of colored or gray-scale rectangles with colors corresponding to the values |
matplot() |
Wrapper of plot() for plotting columns of one matrix against columns of another. |
pairs() |
Generate a matrix of scatterplots. |
persp() |
Generate a perspective plot of a surface |
pie() |
Generate a pie chart. |
plot() |
Generic plotting function, depending on the object’s class. |
stripchart() |
Generate a one dimensional scatter plot. |
stars() |
Generate a star plot or segment diagram of a multivariate data set. |
symbols() |
Generate a plot with symbols (circles, squares, rectangles, stars, thermometers, and boxplots) representing data values. |
ts.plot() |
Plot multiple Time Series. |
abline() |
Add straight lines to a graph. |
arrows() |
Add arrows to a graph. |
axis() |
Add an axis to a graph. |
box() |
Add a surrounding box to a graph. |
legend() |
Add legends to a graph. |
lines() |
Add connected line segments to a graph. |
mtext() |
Add text to the margins of a graph. |
points() |
Add points to a graph. |
polygon() |
Add polygons to a graph. |
rect() |
Add a rectangle to a graph. |
rug() |
Add short lines representing data values to a graph. |
segments() |
Add line segments to a graph. |
text() |
Add text to a graph. |
Common base R plotting functions
7.2 ggplot2
| Function | Description |
|---|---|
geom_abline(), geom_hline(), geom_vline() |
Straight lines. |
geom_area(), geom_ribbon() |
Area and ribbon plot. |
geom_bar() |
Bar chart. |
geom_histogram(), geom_freqpoly(), geom_bin2d(), geom_hex() |
1D/2D histogram or frequency polygons. |
geom_boxplot() |
Box plot. |
geom_contour() |
Contour plot. |
geom_crossbar(), geom_errorbar(), geom_errorbarh(), geom_linerange(),geom_pointrange() |
Data value with intervals. |
geom_density(), geom_density2d() |
Distribution density curve/surface. |
geom_line(), geom_path() |
Line chart. |
geom_point() |
Scatterplot. |
geom_polygon(), geom_rect(), geom_tile() |
Polygons/rectangles. |
geom_rug() |
Rugs. |
geom_segment(), geom_curve() |
Line segments and curves. |
geom_smooth(), geom_quantile() |
Fitted lines. |
geom_text() |
Text. |
Common ggplot plotting functions
ggplot() + # mandatory: data mapping
geom_xxx() + # mandatory: geometry shape
coordinate_xxx() +
theme() +
facet_xxx() +
...7.3 两个例子
library(fecitr)
plot_summary(diet, if_box = TRUE)
library(GGally)
ggpairs(diet)8 延伸阅读
R for Data Science, Chapter 3 and 28